package com.sy.service.message.impl;

import com.sy.bean.message.GoodsCategory;
import com.sy.bean.message.query.Category;
import com.sy.common.crud.BaseServiceImpl;
import com.sy.common.exception.BusinessException;
import com.sy.mapper.message.GoodsCategoryMapper;
import com.sy.service.message.GoodsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsCategoryServiceImpl extends BaseServiceImpl<GoodsCategory, GoodsCategoryMapper> implements GoodsCategoryService {
    @Autowired
    GoodsCategoryMapper goodsCategoryMapper;

    @Override
    public List<GoodsCategory> tree() {
        return goodsCategoryMapper.tree();
    }

    @Override
    public int saveCategory(Category entity) {
        entity.setCode(code(entity.getParentId()));
        int rs=goodsCategoryMapper.saveCategory(entity);
        if (rs==0){
            throw  new BusinessException("新增失败");
        }
        return 1;
    }

    String code(int id) {
        String max = goodsCategoryMapper.maxCode(id);
        if (max == null) {
            max = goodsCategoryMapper.code(id) + "01";
        } else {
            int a = Integer.parseInt(max);
            a++;
            max = a + "";
        }
        String arr=max;
        return arr;
    }
}
